package priv.mill.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import priv.mill.controller.util.RetenObj;
import priv.mill.entry.SysUserInfo;
import priv.mill.server.SysUserInfoService;

@Controller
@RequestMapping("SysUserInfo")
public class SysUserInfoController {

	@Autowired
	private SysUserInfoService sysUserInfoService;

	private static final Logger logger = Logger.getLogger(SysUserInfoController.class);

	@RequestMapping("getSysUserInfoById")
	@ResponseBody
	public RetenObj getSysUserInfoById(@RequestParam("SysUserInfoId") Integer sysUserInfoId) {
		try {
			return warpData(sysUserInfoService.getSysUserInfoById(sysUserInfoId), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("getSysUserInfoByEntry")
	@ResponseBody
	public RetenObj getSysUserInfoByEntry(SysUserInfo sysUserInfo) {
		try {
			return warpData(sysUserInfoService.getSysUserInfoByEntry(sysUserInfo), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("getSysUserInfosByPage")
	@ResponseBody
	public RetenObj getSysUserInfosByPage(SysUserInfo sysUserInfo) {
		try {
			return warpData(sysUserInfoService.getSysUserInfosByPage(sysUserInfo), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("updateSysUserInfo")
	@ResponseBody
	public RetenObj updateSysUserInfo(SysUserInfo sysUserInfo) {
		try {
			int result = sysUserInfoService.updateSysUserInfo(sysUserInfo);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	

	@RequestMapping("insertSysUserInfo")
	@ResponseBody
	public RetenObj insertSysUserInfo(SysUserInfo sysUserInfo) {
		try {
			int result = sysUserInfoService.insertSysUserInfo(sysUserInfo);
			if (result > 0)
				return warpData(sysUserInfo.getUId(), 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("deleteSysUserInfo")
	
	@ResponseBody
	public RetenObj deleteSysUserInfo(Integer sysUserInfoId) {
		try {
			int result = sysUserInfoService.deleteSysUserInfo(sysUserInfoId);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
